package com.jetwinner.webfast.module.bigapp.service.impl;

import com.jetwinner.util.EasyStringUtil;
import com.jetwinner.webfast.kernel.dao.support.OrderBy;
import com.jetwinner.webfast.kernel.exception.RuntimeGoingException;
import com.jetwinner.webfast.module.bigapp.dao.AppGroupDao;
import com.jetwinner.webfast.module.bigapp.dao.AppGroupThreadDao;
import com.jetwinner.webfast.module.bigapp.service.AppGroupService;
import com.jetwinner.webfast.module.bigapp.service.AppGroupThreadService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author xulixin
 */
@Service
public class AppGroupThreadServiceImpl implements AppGroupThreadService {

    private final AppGroupThreadDao threadDao;
    private final AppGroupService groupService;

    public AppGroupThreadServiceImpl(AppGroupThreadDao threadDao, AppGroupDao groupDao, AppGroupService groupService) {
        this.threadDao = threadDao;
        this.groupService = groupService;
    }

    @Override
    public int searchThreadsCount(Map<String, Object> conditions) {
        return threadDao.searchThreadsCount(conditions);
    }

    @Override
    public List<Map<String, Object>> searchThreads(Map<String, Object> conditions, OrderBy orderBy,
                                                   Integer start, Integer limit) {

        return threadDao.searchThreads(conditions, orderBy, start, limit);
    }

    @Override
    public Map<String, Object> addThread(Map<String, Object> thread) {
        if (EasyStringUtil.isBlank(thread.get("title"))) {
            throw new RuntimeGoingException("标题名称不能为空！");
        }
        thread.put("title", EasyStringUtil.purifyHtml(thread.get("title")));

        if (EasyStringUtil.isBlank(thread.get("content"))) {
            throw new RuntimeGoingException("话题内容不能为空！");
        }
        thread.put("content",  EasyStringUtil.purifyHtml(thread.get("content")));

        if (EasyStringUtil.isBlank(thread.get("groupId"))) {
            throw new RuntimeGoingException("小组Id不能为空！");
        }
        if (EasyStringUtil.isBlank(thread.get("userId"))) {
            throw new RuntimeGoingException("用户ID不能为空！");
        }
        thread.put("createdTime", System.currentTimeMillis());
        thread = threadDao.addThread(thread);

        // groupService.waveGroup(thread.get("groupId"), "threadNum", +1);

        // groupService.waveMember(thread.get("groupId"), thread.get("userId"), "threadNum", +1);

        // this.hideThings(thread.get("content"), thread.get("id"));

        return thread;
    }

    @Override
    public Map<String, Object> getThread(Integer threadId) {
        return threadDao.getThread(threadId);
    }

    @Override
    public boolean isCollected(Integer userId, Object threadId) {
        return false;
    }

    @Override
    public int searchPostsCount(Map<String, Object> condition) {
        return 0;
    }

    @Override
    public List<Map<String, Object>> searchPosts(Map<String, Object> condition, OrderBy orderBy, Integer start, Integer limit) {
        return null;
    }

    @Override
    public List<Map<String, Object>> searchGoods(Map<String, Object> condition, OrderBy orderBy, Integer start, Integer limit) {
        return null;
    }

    @Override
    public void waveHitNum(Integer threadId) {

    }

    @Override
    public Map<String, Object> getTradeByUserIdAndThreadId(Integer userId, Object threadId) {
        return null;
    }
}
